import sys
import os
import random
import logging
#from collections import Set

logger = logging.getLogger("dabble")

class SpellChecker(object):
    """
    This class is a wrapper for loading and checking of words! 
    Must cache the list of words, as the file with words is >10MB.
    """

    _wordlist = None

    def __init__(self, wordlist):
        if not os.path.isfile(wordlist):
            raise IOError("%s is not a file" % wordlist)

        with open(wordlist,'r') as f:
            lines = (x.replace('\n','') for x in f.readlines())
            self._wordlist = set(lines)

    def check_word(self, word):
        word = word.encode('utf-8') # ensure that we use utf-8 words!
        if word in self._wordlist:
            logger.debug("Word %s is a norwegian word!" % word)
            return True

        logger.debug("Word %s is NOT a norwegian word!" % word)
        return False
